package com.barneyx.mshinfoplatform.web.admin.admin;

import cn.hutool.core.collection.ListUtil;
import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.entity.admin.AreaAdminUserMapEntity;
import com.barneyx.mshinfoplatform.entity.area.AreaEntity;
import com.barneyx.mshinfoplatform.exception.AdminUserException;
import com.barneyx.mshinfoplatform.util.AreaUtil;
import com.barneyx.mshinfoplatform.util.admin.AdminUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/admin/area/")
@Slf4j
public class AdminAreaController {
    @Resource
    private AdminUtil adminUtil;

    @Resource
    private AreaUtil areaUtil;


    /**
     * 获取指定管理员所控制的地区列表
     *
     * @param adminUserId
     * @return
     * @throws AdminUserException
     */
    @GetMapping("user-area-list")
    public ExResult<?> getUserAreaList(@RequestParam("adminid") String adminUserId) throws AdminUserException {
        return ExResult.ofAuto(adminUtil.getAreaAdminListByAdminUserId(adminUserId));
    }

    /**
     * 删除指定的管理员和地区映射信息
     *
     * @param areaId
     * @param adminId
     * @return
     */
    @DeleteMapping("delete-user-area")
    public ExResult<?> deleteUserAreaById(@RequestParam("areaid") String areaId,
                                          @RequestParam("adminid") String adminId) {
        adminUtil.deleteUserAreaMapById(adminId, areaId);
        return ExResult.ofSuccess(true);
    }

    @GetMapping("area-list")
    public ExResult<?> getAreaList() {
        return ExResult.ofAuto(areaUtil.getAreaList());
    }

    @PostMapping("save-user-area-data")
    public ExResult<?> saveUserAreaData(@RequestParam("adminid") String adminId,
                                        @RequestBody List<String> list) {
        List<AreaAdminUserMapEntity> _list = new ArrayList<>();
        list.forEach(areaId -> _list.add(AreaAdminUserMapEntity.builder()
                .UserId(adminId)
                .AreaId(areaId)
                .build()));
        boolean areaAdminMapData = adminUtil.createAreaAdminMapData(_list);
        return ExResult.ofAuto(areaAdminMapData);
    }


}
